package itbour.onetouchshow.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import itbour.onetouchshow.R;

/**
 * Created by guoh on 2018/8/7.
 * 功能描述：包含着字幕与 操作图标的控件
 * 需要的参数：
 */
public class FontWithOperationLayout extends RelativeLayout {

    //缩放图标宽高
    private int bitMapWidth;
    private int bitMapHeight;
    private FontTextView fontTextView;
    private ImageView ivScale;
    //点击间隔时间
    private long mClickTime = 0;
    //点击的框坐标
    private int touchLayoutLastX;
    private int touchLayoutLastY;
    //字体
    private FontAttributeSettingLayout.Font font;

    //字体颜色
    private Integer r = 255;
    private Integer g = 255;
    private Integer b = 255;

    //视频中的X Y实际坐标
    private int realTopMargin;
    private int realLeftMargin;
    //默认文字大小
    private int textSize = 14;
    //缩放比例
    private float videoScale;
    //父布局宽高
    private int parentHeight;
    private int parentWidth;
    //控件中字幕的宽高
    private int mLetterWidth;
    private int mLetterHeight;
    //判断是否在原来文字的宽度上增加了一部分宽度
    //增加的部分宽度为了能多显示出部分文字 以提示用户文字已经输入
    private boolean hasAddWidth;
    //字幕添加时间
    private float letterStartTime;
    //字幕结束时间
    private float letterEndTime;
    private float letterDuration;
    private onDoubleClickListener onDoubleClickListener;

    public FontWithOperationLayout(Context context) {
        super(context);
        initView(context);
    }

    public FontWithOperationLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public FontWithOperationLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public float getLetterDuration() {
        return letterDuration;
    }

    public void setLetterDuration(float letterDuration) {
        this.letterDuration = letterDuration;
    }


    //    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int x = (int) event.getX();
//        int y = (int) event.getY();
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                // 记录触摸点坐标
//                touchLayoutLastX = x;
//                touchLayoutLastY = y;
//                if(System.currentTimeMillis()-mClickTime<800){
//                    //双击回调
//                    if (onDoubleClickListener!=null){
//                        onDoubleClickListener.onDoubleClick(this);
//                    }
//                }
//                else{
//                    mClickTime=System.currentTimeMillis();
//                    //单击回调
//                    if (onDoubleClickListener!=null){
//                        onDoubleClickListener.onSingleClick(this);
//                    }
//                }
//                break;
//            case MotionEvent.ACTION_MOVE:
//                // 计算偏移量
//                int offsetX = x - touchLayoutLastX;
//                int offsetY = y - touchLayoutLastY;
//                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) this.getLayoutParams();
//                params.topMargin=params.topMargin+offsetY;
//                params.leftMargin=params.leftMargin+offsetX;
//                params.rightMargin=-parentWidth;
//                params.bottomMargin=-parentHeight;
//                setRealLeftMargin((int) (params.leftMargin*videoScale));
//                setRealTopMargin((int) (params.topMargin*videoScale));
//                this.setLayoutParams(params);
//                break;
//        }
//        return true;
//    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_font_with_operation, this);
        fontTextView = view.findViewById(R.id.font_textview);
        ivScale = view.findViewById(R.id.iv_scale);
        ivScale.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                v.getParent().getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        RelativeLayout.LayoutParams params = (LayoutParams) fontTextView.getLayoutParams();
                        params.width += event.getX();
                        params.height += event.getY();
                        //最小宽高限制
                        if (params.width < bitMapWidth) params.width = bitMapWidth;
                        if (params.height < bitMapHeight) params.height = bitMapHeight;
                        fontTextView.setLayoutParams(params);
                        mLetterWidth = params.width;
                        mLetterHeight = params.height;
                        setFixedLayoutWidthAndHeight(params.width + bitMapWidth / 2, params.height + bitMapHeight / 2);
                        if (hasAddWidth)
                            hasAddWidth = false;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_font_scale);
        bitMapWidth = bitmap.getWidth();
        bitMapHeight = bitmap.getHeight();

        //使用手势监听取代OnTouch监听中的逻辑 拖动与双击逻辑区分更加明显
        GestureDetectorCompat detectorCompat = new GestureDetectorCompat(getContext(), new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                // 计算偏移量
                int offsetX = (int) (e2.getX() - e1.getX());
                int offsetY = (int) (e2.getY() - e1.getY());
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) FontWithOperationLayout.this.getLayoutParams();
                params.topMargin = params.topMargin + offsetY;
                params.leftMargin = params.leftMargin + offsetX;
                params.rightMargin = -parentWidth;
                params.bottomMargin = -parentHeight;
                setRealLeftMargin((int) (params.leftMargin * videoScale));
                setRealTopMargin((int) (params.topMargin * videoScale));
                FontWithOperationLayout.this.setLayoutParams(params);
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }
        });
        detectorCompat.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (onDoubleClickListener != null) {
                    onDoubleClickListener.onSingleClick(FontWithOperationLayout.this);
                }
                return false;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                if (onDoubleClickListener != null) {
                    onDoubleClickListener.onDoubleClick(FontWithOperationLayout.this);
                }
                return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                return false;
            }
        });

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                detectorCompat.onTouchEvent(event);
                return true;
            }
        });
    }

    public void setOnDoubleClickListener(onDoubleClickListener onDoubleClickListener) {
        this.onDoubleClickListener = onDoubleClickListener;
    }

    //设置选中样式
    public void setLayoutSelect(boolean select) {
        setSelected(select);
        ivScale.setVisibility(select ? VISIBLE : INVISIBLE);
        fontTextView.setBackground(getResources().getDrawable(R.drawable.letter_style));
        fontTextView.setSelected(select);
    }

    //设置透明样式
    public void setLayoutTransparent() {
        fontTextView.setBackground(getResources().getDrawable(R.drawable.font_unselect_translate));
        ivScale.setVisibility(GONE);
    }

    public int getLetterRealWidth() {
        return (int) (mLetterWidth * videoScale);
    }

    public int getLetterRealHeight() {
        return (int) (mLetterHeight * videoScale);
    }

    public float getVideoScale() {
        return videoScale;
    }

    public void setVideoScale(float videoScale) {
        this.videoScale = videoScale;
    }

    public FontTextView getFontTextView() {
        return fontTextView;
    }

    public float getLetterStartTime() {
        return letterStartTime;
    }

    public void setLetterStartTime(float letterStartTime) {
        this.letterStartTime = letterStartTime;
    }

    public float getLetterEndTime() {
        return letterEndTime;
    }

    public void setLetterEndTime(float letterEndTime) {
        this.letterEndTime = letterEndTime;
    }

    public FontAttributeSettingLayout.Font getFont() {
        if (font == null) font = FontAttributeSettingLayout.Font.defaultFont;
        return font;
    }

    //设置字体
    public void setFont(FontAttributeSettingLayout.Font font) {
        this.font = font;
        switch (font) {
            case defaultFont:
                fontTextView.setDefaultFontStyle();
                break;
            case blackFont:
                fontTextView.setBlackFontStyle();
                break;
        }
    }

    public int getBitMapWidth() {
        return bitMapWidth;
    }

    public int getBitMapHeight() {
        return bitMapHeight;
    }

    public String getText() {
        return fontTextView.getText().toString();
    }

    public void setText(String str) {
        fontTextView.setText(str);
    }

    public Integer getR() {
        return r;
    }

    public Integer getG() {
        return g;
    }

    public Integer getB() {
        return b;
    }

    public void setFontTextColor(Integer r, Integer g, Integer b) {
        this.r = r;
        this.g = g;
        this.b = b;
        fontTextView.setTextColor(Color.rgb(r, g, b));
    }

    public int getRealTopMargin() {
        return realTopMargin;
    }

    public void setRealTopMargin(int realTopMargin) {
        this.realTopMargin = realTopMargin;
    }

    public int getRealLeftMargin() {
        return realLeftMargin;
    }

    public void setRealLeftMargin(int realLeftMargin) {
        this.realLeftMargin = realLeftMargin;
    }

    public int getFontTextSize() {
        return textSize;
    }

    public void setFontTextSize(int size) {
        //默认14sp
        this.textSize = size;
        fontTextView.setTextSize(size);
    }

    public boolean isHasAddWidth() {
        return hasAddWidth;
    }

    public void setHasAddWidth(boolean hasAddWidth) {
        this.hasAddWidth = hasAddWidth;
    }

    public void setParentHeight(int parentHeight) {
        this.parentHeight = parentHeight;
    }

    public void setParentWidth(int parentWidth) {
        this.parentWidth = parentWidth;
    }

    //设置Layout固定的宽高
    public void setFixedLayoutWidthAndHeight(int width, int height) {
        RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
        params.width = width;
        params.height = height;
        this.setLayoutParams(params);
    }

    //设置文本固定的宽高
    public void setFixedTextViewWidthAndHeight(int width, int height) {
        mLetterWidth = width;
        mLetterHeight = height;
        RelativeLayout.LayoutParams textParams = (LayoutParams) fontTextView.getLayoutParams();
        textParams.width = width;
        textParams.height = height;
        fontTextView.setLayoutParams(textParams);
    }

    //重置图标的位置
    public void resetImageLocation() {
        RelativeLayout.LayoutParams params = (LayoutParams) ivScale.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        ivScale.setLayoutParams(params);
        ivScale.setLayoutParams(params);
    }

    public interface onDoubleClickListener {
        void onDoubleClick(View view);

        void onSingleClick(View view);
    }
}
